// DlgWaitLaser.cpp : implementation file
//

#include "stdafx.h"
#include "DlgWaitLaser.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CDlgWaitLaser dialog


CDlgWaitLaser::CDlgWaitLaser(CWnd* pParent /*=NULL*/)
	: CDialog(CDlgWaitLaser::IDD, pParent)
{
	//{{AFX_DATA_INIT(CDlgWaitLaser)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	m_iWaitSeconds = -1;
	m_iWaitType = -1;
}


void CDlgWaitLaser::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CDlgWaitLaser)
		// NOTE: the ClassWizard will add DDX and DDV calls here
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CDlgWaitLaser, CDialog)
	//{{AFX_MSG_MAP(CDlgWaitLaser)
	ON_WM_TIMER()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CDlgWaitLaser message handlers

void CDlgWaitLaser::OnTimer(UINT nIDEvent) 
{
	// TODO: Add your message handler code here and/or call default
	if (nIDEvent == 12345)
	{
		if (m_iWaitType == 0)
		{
			if (pNlight->IsIDle())
			{
				if ((m_iCurSeconds <= m_iWaitSeconds))
				{
					EndDialog(IDOK);
				}
				else
				{
					EndDialog(IDCANCEL);
				}
			}
			else
			{
				if ((m_iCurSeconds > m_iWaitSeconds))
				{
					EndDialog(IDCANCEL);
				}
				else
				{
					CString strWait;
					strWait.Format(_T("Please waiting for laser idle: %ds"),m_iCurSeconds);
					GetDlgItem(IDC_STATIC_WaitString)->SetWindowText(strWait);
				}
			}	
		}
		else if (m_iWaitType == 1)
		{
			if (pNlight->IsReady())
			{
				if ((m_iCurSeconds <= m_iWaitSeconds))
				{
					EndDialog(IDOK);
				}
				else
				{
					EndDialog(IDCANCEL);
				}
			}
			else
			{
				if ((m_iCurSeconds > m_iWaitSeconds))
				{
					EndDialog(IDCANCEL);
				}
				else
				{
					CString strWait;
					strWait.Format(_T("Please waiting for laser ready: %ds"),m_iCurSeconds);
					GetDlgItem(IDC_STATIC_WaitString)->SetWindowText(strWait);
				}
			}		
		}
		m_iCurSeconds++;
	}
	CDialog::OnTimer(nIDEvent);
}

BOOL CDlgWaitLaser::OnInitDialog() 
{
	CDialog::OnInitDialog();
	
	// TODO: Add extra initialization here
	if (m_iWaitSeconds > 0)
	{
		m_iCurSeconds = 1;
		SetTimer(12345,1000,NULL);
	}
	else
	{
		EndDialog(-1);
	}
	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

void CDlgWaitLaser::SetParam(int iWaitType, int iWaitSeconds, CNLightProtocol *pControl)
{
	m_iWaitType = iWaitType;
	m_iWaitSeconds = iWaitSeconds;
	pNlight = pControl;
}
